package com.daocao.jy.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.jy.R;
import com.daocao.jy.api.model.RechargeInstantModel;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class RechargeTypeAdapter extends ItemAdapter<RechargeInstantModel.DatasBean.ItemBean, RechargeTypeAdapter.DefaultHolder> {




    public RechargeTypeAdapter(Context context) {
        super(context);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_recharge_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        RechargeInstantModel.DatasBean.ItemBean item = getItem(position);
        if (item.isCheck) {
            holder.txtMoney.setTextColor(0xffFB5E01);
            holder.txtMoneySec.setTextColor(0xffFB5E01);
        } else {
            holder.txtMoney.setTextColor(0xff000000);
            holder.txtMoneySec.setTextColor(0xff666666);
        }
        holder.txtMoney.setText(item.money + "元");
        holder.txtMoneySec.setText("售价" + item.sale_money + "元");
        if (position == getItemCount() - 1) {
            holder.txtMoneySec.setVisibility(View.GONE);
            holder.txtMoney.setText("+  自定义");
        }else {
            holder.txtMoneySec.setVisibility(View.VISIBLE);
        }

        holder.layoutTotal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.money, item.sale_money);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_money)
        TextView txtMoney;
        @BindView(R.id.txt_money_sec)
        TextView txtMoneySec;
        @BindView(R.id.layout_total)
        LinearLayout layoutTotal;
        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String money, String sale_money);
    }
}
